/*
 * Background Flickr: Copyright (C) 2009  Craig McNicholas
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ddw.app.bflickr.flickr.api.url;

import com.ddw.app.bflickr.logging.Log;
import com.ddw.app.bflickr.logging.LogFactory;


/**
 * Class used to build a flickr find by username 
 * REST url ready for requesting data.
 * This class will create a url which searches for 
 * a users flickr account NSID by supplying a valid 
 * flickr username.
 * @author craig
 *
 */
public class FlickrFindByUsernameURL extends FlickrApiURL implements FlickrFindURL {

	/**
	 * The service method name to use in the query string of a flickr api 
	 * REST service call.
	 */
	public static final String SERVICE_METHOD = "flickr.people.findByUsername";
	
	/**
	 * The username query string and item key referring to the username 
	 * held in the extra parameters.
	 */
	private static final String KEY_USERNAME = "username";
	
	private Log log = LogFactory.createInstance(this.getClass());
	
	/**
	 * Creates a new flickr api url instance for using the REST api find by username 
	 * service available at flickr.com. This url is used to request a search for a
	 * flickr account matching the username and returning the NSID of the account.
	 * @param apiKey the unique application key
	 * @param username the username to search for
	 * @throws IllegalArgumentException when the api key is null or username is null or empty, <i>api keys cannot be null and the username cannot be null or empty</i>
	 */
	public FlickrFindByUsernameURL(String apiKey, String username) throws IllegalArgumentException {
		super(FlickrApiMethod.FIND_BY_USERNAME, apiKey);
		setUsername(username);
	}
	
	/**
	 * Getter of the username to search for on flickr
	 * @return the username to append to the querystring
	 */
	public String getUsername() {
		if(hasExtraParam(KEY_USERNAME)) {
			return this.getExtraParams().get(KEY_USERNAME);
		}
		log.warn("cannot find extra parameter " + KEY_USERNAME);
		return null;
	}
	
	/**
	 * Setter of the username to search for on flickr
	 * @return the username to append to the querystring
	 */
	public void setUsername(String username) {
		if(username == null) {
			log.error("The flickr username cannot be null");
			throw new IllegalArgumentException("The flickr username cannot be null");
		}
		if(username.trim().length() == 0) {
			log.error("The flickr username cannot be empty");
			throw new IllegalArgumentException("The flickr username cannot be empty");
		}
		setExtraParam(KEY_USERNAME, username);
	}

}
